<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

/**
 * 系统日志表模型
 * Author: Albert3306
 * Date: 2020/6/12
 * @package App\Models
 */
class SystemLog extends Model
{
    protected $fillable = ['user_id', 'type', 'url', 'content', 'operator_ip'];

    /**
     * 操作用户
     * 模型对象关系：系统日志对应的操作用户
     * Author: Albert3306
     * Date: 2020/6/12
     * @return BelongsTo
     */
    public function user()
    {
        return $this->belongsTo('App\Models\User', 'user_id', 'id');
    }

    /**
     * 记录系统日志
     * Author: Albert3306
     * Date: 2020/6/12
     * @param  array  $data 需要写入的数据
     * @return bool
     */
    public function write($data)
    {
        $data['operator_ip'] = app('request')->ip();
        $this->fill($data);
        return $this->save($data);
    }
}
